<?php
/**
 * 产品数据处理
 *
 * @author Akon(番茄红了) <aultoale@gmail.com>
 */
class Sync_Products
{

	/**
	 * 获取远程数据错误信息
	 *
	 * @param  array  $data
	 * @return string
	 */
	public static function remoteException($data, $func = null, $suffix = null)
	{
		if (isset($data['error_response']))
		{

			$error = $data['error_response'];

			$msg = isset($error['sub_msg']) ? $error['sub_msg'] : (isset($error['msg']) ? $error['msg'] : '未知错误');
			$code = isset($error['code']) ? $error['code'] : 0;

			throw new Sync_Exception($msg, $code, $func, $suffix);
		}
	}

	/**
	 * 获取远程产品数量统计
	 *
	 * @param  int  $cid
	 * @return int
	 * @throws Exception
	 */
	public static function remoteTotal($cid)
	{
		$api = new Taoapi();
		$api->method = 'taobao.products.search';
		$api->fields = 'product_id';
		$api->page_no = 1;
		$api->page_size = 1;
		$api->cid = $cid;

		$errorSuffix = "分类ID：{$cid}";

		for ($i = 1; $i <= 10; $i++)
		{
			$data = $api->send('get','json')->getArrayData();
			try {
				self::remoteException($data, __FUNCTION__, __CLASS__);
				if (isset($data['products_search_response']['total_results'])) {
					return $data['products_search_response']['total_results'];
				}
				CLI::output('.');
				throw new Sync_Exception("第 $i 次获取产品数量统计失败", $errorSuffix, __FUNCTION__, __CLASS__);
			} catch (Sync_Exception $e) {
				$e->code() && CLI::output('>');
				sleep(20); // 休息10 秒钟
			}
		}
	}

	/**
	 * 获取远程产品数据列表
	 *
	 * @param  int  $cid
	 * @param  int  $page_no
	 * @param  int  $page_size
	 * @return array
	 * @throws Exception
	 */
	public static function remoteList($cid, $page_no, $page_size)
	{
		$api = new Taoapi();
		$api->method = 'taobao.products.search';
		$api->fields = 'product_id';
		$api->page_no = $page_no;
		$api->page_size = $page_size;
		$api->cid = $cid;

		$errorSuffix = "分类ID：{$cid}，页码：{$page_no}，每页记录数：{$page_size}";

		for ($i = 1; $i <= 10; $i++)
		{
			$data = $api->send('get','json')->getArrayData();
			try {
				self::remoteException($data, __FUNCTION__, __CLASS__);
				if (isset($data['products_search_response']['products']['product'])) {
					return $data['products_search_response']['products']['product'];
				}
				CLI::output('.');
				throw new Sync_Exception("第 $i 次获取产品数据列表失败", $errorSuffix, __FUNCTION__, __CLASS__);
			} catch (Sync_Exception $e) {
				$e->code() && CLI::output('>');
				sleep(20); // 休息10 秒钟
			}
		}
	}

	/**
	 * 获取远程产品数据
	 *
	 * @param  int  $product_id
	 * @return array
	 * @throws Exception
	 */
	public static function remoteData($product_id)
	{
		$api = new Taoapi();
		$api->method = 'taobao.product.get';
		$api->fields = 'product_id,outer_id,cid,cat_name,props,props_str,'.
					   'name,binds,binds_str,sale_props,sale_props_str,desc,'.
					   'pic_url,pic_path,price,tsc,shop_price,standard_price,'.
					   'sale_num,rate_num,level,collect_num,status,modified,'.
					   'created,vertical_market,customer_props,property_alias,'.
					   'product_imgs,product_prop_imgs';
		$api->product_id = $product_id;

		$errorSuffix = "产品ID：{$product_id}";

		for ($i = 1; $i <= 10; $i++)
		{
			$data = $api->send('get','json')->getArrayData();
			try {
				self::remoteException($data, __FUNCTION__, __CLASS__);
				if (isset($data['product_get_response']['product'])) {
					return $data['product_get_response']['product'];
				}
				CLI::output('.');
				throw new Sync_Exception("第 $i 次获取产品数据失败", $errorSuffix, __FUNCTION__, __CLASS__);
			} catch (Sync_Exception $e) {
				$e->code() && CLI::output('>');
				sleep(20); // 休息10 秒钟
			}
		}
	}

}